package com.nulldev.util.web.HttpClient.clients.nWS;

import java.net.URI;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import com.nulldev.util.data.Arrays.maps.FastMaps;
import com.nulldev.util.web.HttpClient.providers.websockets.HttpWebSocket;
import com.nulldev.util.web.HttpClient.providers.websockets.HttpWebSocketRequest;
import com.nulldev.util.web.URLUtils.Entry;

public class nWSSocketRequest implements HttpWebSocketRequest {

	private final Map<String, String> headers = FastMaps.map();
	private final AtomicReference<String> url = new AtomicReference<String>();

	public nWSSocketRequest() {
	}

	@Override
	public HttpWebSocketRequest url(String url) {
		this.url.set(url);
		return this;
	}

	@Override
	public HttpWebSocketRequest url(URL url) {
		this.url.set(url.toString());
		return this;
	}

	@Override
	public HttpWebSocketRequest url(URI url) {
		this.url.set(url.toString());
		return this;
	}

	@Override
	public HttpWebSocket make() throws Exception {
		return new nWSSockets(this.url.get(), this.headers);
	}

	@Override
	public HttpWebSocketRequest header(final String key, final String value) {
		if (key == null || key.isEmpty() || value == null)
			return this;
		this.headers.put(key, value);
		return this;
	}

	@Override
	public HttpWebSocketRequest header(final Entry e) {
		if (e != null && e.isValid())
			this.headers.put(e.getKey(), e.getValue());
		return this;
	}

	@Override
	public HttpWebSocketRequest headers(final Entry... es) {
		if (es == null || es.length == 0)
			return this;
		for (final Entry e : es) {
			if (e.isValid())
				this.headers.put(e.getKey(), e.getValue());
		}
		return this;
	}

}
